package com.addremoveitemtest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddRemove extends AppCompatActivity {

    @BindView(R.id.recycler4)
    RecyclerView mRecycler4;
    @BindView(R.id.textContent)
    TextView mTextContent;
    private List<AddRemoveEntity> addListSum = new ArrayList<>();
    private List<AddRemoveEntity> addList1 = new ArrayList<>();
    private List<AddRemoveEntity> addList2;
    private AddRemoveAdapter mAddRemoveAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_remove);
        ButterKnife.bind(this);
        initData();
        init();
    }

    private void initData() {
        addList2 = AddRemoveEntity.entityList;//给集合2
        addList2.removeAll(addList1);//
        addListSum.add(null);//可以看作"选择item"标题
        addListSum.addAll(addList1);//可以看作上面的item集合
        addListSum.add(null);//可以看作"总的item"标题
        addListSum.addAll(addList2);//可以看作下面的item集合
    }

    private void init() {

        mAddRemoveAdapter = new AddRemoveAdapter(this, addList1, addListSum);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        mRecycler4.setLayoutManager(gridLayoutManager);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                /*控制每行多少item*/
                switch (mAddRemoveAdapter.getItemViewType(position)) {
                    case AddRemoveAdapter.TYPE0:
                    case AddRemoveAdapter.TYPE1:
                        //返回为3，则ItemViewType为TYPE0和TYPE1时，鉴于上面设置的3，对应的View占3/3行，也就是"选择item"和"总的item"标题各占一行
                        return 3;
                }
                //返回为1，则ItemViewType，鉴于上面设置的3，对应的View占1/3行，也就是选择item占1/3行，也就是三个item占一行
                return 1;
            }
        });
        mRecycler4.setAdapter(mAddRemoveAdapter);
        mAddRemoveAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*点击每个item*/
                switch (v.getId()) {
                    case R.id.btn_label:
                      /*在此处获得Adapter里面设置的标记并判断位置*/
                        int id = (int) v.getTag();
                        /*获取id在上面集合中的位置*/
                        int i = getIById(id, addList1);
                        /*如果这个id在上面集合中*/
                        if (i != -1) {
                            AddRemoveEntity addRemoveEntity = addList1.remove(i);//将点击的此item从上面集合删除
                            addList2.add(addRemoveEntity);//下面集合中
                            addListSum.remove(i + 1);//从总的集合中把此item删除
                            addListSum.add(addRemoveEntity);//插入到总集合的最后一个，注：在集合最后一个位置插入元素可不用指定位置
                            mAddRemoveAdapter.notifyItemMoved(i + 1, addListSum.size() - 1);//通知adapter此item从i + 1位置移动到addListSum.size() - 1位置
                            mAddRemoveAdapter.notifyItemChanged(addListSum.size() - 1);//刷新adapter位置变化
                        } else {
                            int i2 = getIById(id, addList2);//查询是否下面集合中有该item，并返回位置索引
                            if (i2 != -1) {//如果存在
                                AddRemoveEntity removeEntity = addList2.remove(i2);//将此item下面集合中移除
                                addList1.add(removeEntity);//加入上面集合中
                                addListSum.remove(1 + addList1.size() - 1 + 1 + i2);//从总的集合中移除
                                //并加入总的集合中，上面集合的最后一个位置（总的集合包括上面和下面集合），注：在总的集合中间插入元素一定要指定位置
                                addListSum.add(addList1.size(), removeEntity);
                                /*通知adapter此item从1 + addList1.size() - 1 + 1 + i2位置移动到 1 + addList1.size() - 1位置
                                其中第一个1代表总集合中的第一null，也就是"选择的item"标题，第三个1代表总集合中的第二null，也就是"总的item"标题*/
                                mAddRemoveAdapter.notifyItemMoved(1 + addList1.size() - 1 + 1 + i2, 1 + addList1.size() - 1);
                                mAddRemoveAdapter.notifyItemChanged(1 + addList1.size() - 1);//刷新adapter位置变化
                            }
                        }
                }
            }
        });
    }

    /**
     * 根据id获取点击按钮在集合中对应的位置i
     *
     * @param id
     * @param listCur
     * @return
     */
    public int getIById(int id, List<AddRemoveEntity> listCur) {
        for (int i = 0; i < listCur.size(); i++) {
            if (id == listCur.get(i).getId()) {
                return i;
            }
        }
        return -1;
    }

    public void btnOk(View view) {
        ArrayList<String>strListCur=new ArrayList<>();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < addList1.size(); i++) {
            stringBuffer.append(addList1.get(i).getName() + "\n");
            strListCur.add(addList1.get(i).getName());
        }
        mTextContent.setText(stringBuffer.toString());
        Intent intentRe=new Intent(this,MainActivity.class);
        intentRe.putStringArrayListExtra("strListCur",strListCur);
        this.setResult(101,intentRe);
        finish();
    }

}
